<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="房间类型名称" prop="roomTypeName">
        <el-input
          v-model="queryParams.roomTypeName"
          placeholder="请输入房间类型名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:type:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:type:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:type:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:type:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="typeList" @selection-change="handleSelectionChange">
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="备注">
              <span>{{ props.row.remark }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="房间类型名称" align="center" prop="roomTypeName" />
      <el-table-column label="房间价格" align="center" prop="roomTypePrice" />
      <el-table-column label="房间床型" align="center" prop="roomTypeBedType" />
      <el-table-column label="房间可住人数" align="center" prop="roomTypePeopleNumber" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:type:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:type:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="viewImg(scope.row)"
          >查看图片</el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改客房类型对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">

        <el-form-item label="房间类型名称" prop="roomTypeName">
          <el-input v-model="form.roomTypeName" placeholder="请输入房间类型名称" />
        </el-form-item>
        <el-form-item label="房间价格" prop="roomTypePrice">
          <el-input v-model.number="form.roomTypePrice" placeholder="请输入房间价格" />
        </el-form-item>
        <el-form-item label="房间可住人数" prop="roomTypePeopleNumber">
          <el-input v-model="form.roomTypePeopleNumber" placeholder="请输入房间可住人数" />
        </el-form-item>

        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="房间类型图片" prop="roomTypePhoto">
          <image-upload ref="image" @input="handleSuccess" :limit:="1" ></image-upload>
        </el-form-item>
        <img v-if="imgUrl!=''"
             :src="imgUrl"
             style="display: block; max-width: 100%; margin: 0 auto"
        />
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <el-dialog
      :visible.sync="dialogVisible"
      title="预览"
      width="800"
      append-to-body
    >
      <img
        :src="dialogImageUrl"
        style="display: block; max-width: 100%; margin: 0 auto"
      />
    </el-dialog>
  </div>
</template>

<script>
import { listType, getType, delType, addType, updateType } from "@/api/hotel/type";

export default {
  name: "Type",
  data() {
    let checkNumber = (rule, value, callback) => {
      if ( !value ) {
        return callback (new Error ('价格不能为空'))
      }
      setTimeout (() => {
        if ( !Number.isInteger (value) ) {
          callback (new Error ('请输入数字值'))
        } else {
          callback ()
        }
      }, 1000)
    }
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客房类型表格数据
      typeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        roomTypeNumber: null,
        roomTypeName: null,
        roomTypePrice: null,
        roomTypeBedType: null,
        roomTypePeopleNumber: null,
        roomTypePhoto: null,
        sortIndex: null,
        createrId: null,
        createrName: null,
        updaterId: null,
        updaterName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        roomTypeName: [
          { required: true, message: "房间类型名称不能为空", trigger: "blur" },
        ],
        roomTypePrice: [
          { validator: checkNumber, trigger: 'blur', required: true }
        ],

      },
      imgUrl:'',
      dialogVisible: false,
      dialogImageUrl:''
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询客房类型列表 */
    getList() {
      this.loading = true;
      listType(this.queryParams).then(response => {
        this.typeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        roomTypeNumber: null,
        roomTypeName: null,
        roomTypePrice: null,
        roomTypeBedType: null,
        roomTypePeopleNumber: null,
        roomTypePhoto: null,
        remark: null,
        sortIndex: null,
        createTime: null,
        createrId: null,
        createrName: null,
        updaterId: null,
        updaterName: null
      };
      this.imgUrl = ''
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客房类型";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getType(id).then(response => {
        this.form = response.data;
        this.imgUrl = process.env.VUE_APP_BASE_API + response.data.roomTypePhoto
        this.open = true;
        this.title = "修改客房类型";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateType(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addType(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
      this.$ref.image.reset()
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除客房类型编号为"' + ids + '"的数据项？').then(function() {
        return delType(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/type/export', {
        ...this.queryParams
      }, `type_${new Date().getTime()}.xlsx`)
    },
    handleSuccess(data){
      this.form.roomTypePhoto = data
    },
    viewImg(data){
      this.dialogImageUrl = process.env.VUE_APP_BASE_API+data.roomTypePhoto;
      this.dialogVisible = true;
    }
  }
};
</script>
<style>
  .demo-table-expand {
    font-size: 0;
  }
  .demo-table-expand label {
    width: 90px;
    color: #99a9bf;
  }
  .demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 100%;
  }
</style>
